﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class SysConfigDataAccess
    {
        #region 单例模式
        private SysConfigDataAccess()
        {
        }

        private static SysConfigDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static SysConfigDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new SysConfigDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 获取转发配置
        /// </summary>
        /// <returns></returns>
        public List<SysConfig> GetList(long uid)
        {
            List<SysConfig> list = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string strSql = "select * from ADZ_Sysconfig where uid=@uid order by config_type asc";
                DbCommand Procdbcomm = db.GetSqlStringCommand(strSql);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    list = new ModelHandler<SysConfig>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取转发配置-GetList" + ex.ToString());
            }
            return list;
        }

        /// <summary>
        /// 保存转发配置
        /// </summary>
        /// <returns></returns>
        public long Write(long uid, int config_type, string config_code, string config_name, string config_content, out string stroutput)
        {
            long ret = 0;
            stroutput = "保存失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_sysconfig_write";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@config_type", DbType.Int32, config_type);
                db.AddInParameter(Procdbcomm, "@config_code", DbType.String, config_code);
                db.AddInParameter(Procdbcomm, "@config_name", DbType.String, config_name);
                db.AddInParameter(Procdbcomm, "@config_content", DbType.String, config_content);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("保存转发配置-Write" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 获取版本信息
        /// </summary>
        /// <param name="system_type">系统类型 1 android 2 ios</param>
        /// <returns></returns>
        public GradeInfo GetGradeInfo(int system_type)
        {
            GradeInfo info = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sql = "select top 1 * from ADZ_APPUpGradeInfo where SystemType=@system_type and IsDel=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sql);
                db.AddInParameter(Procdbcomm, "@system_type", DbType.Int32, system_type);

                DataSet ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    info = new ModelHandler<GradeInfo>().FillModel(ds.Tables[0].Rows[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取版本信息-GetGradeInfo" + ex.ToString());
            }
            return info;
        }

        /// <summary>
        /// 获取轮播广告配置
        /// </summary>
        /// <param name="num">广告条数</param>
        /// <returns></returns>
        public List<AdConfig> GetAd(int num)
        {
            List<AdConfig> list = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sql = "select top (@num) * from ADZ_AdConfig where is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sql);
                db.AddInParameter(Procdbcomm, "@num", DbType.Int32, num);

                DataSet ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count != 0)
                    list = new ModelHandler<AdConfig>().FillModel(ds.Tables[0], false, false);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取版本信息-GetAd" + ex.ToString());
            }
            return list;
        }
    }
}
